package org.jeecg.modules.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.golf.plan.entity.Plan;
import org.jeecg.modules.golf.plan.service.IPlanService;
import org.jeecg.modules.golf.question.client.qto.QuestionQueryPageDTO;
import org.jeecg.modules.golf.question.entity.Question;
import org.jeecg.modules.golf.question.service.IQuestionService;
import org.jeecg.modules.golf.question.service.IQuestionTypeService;
import org.jeecg.modules.golf.user.client.TraineeAgeDistDTO;
import org.jeecg.modules.golf.user.service.ITraineeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/main")
public class IndexController {
    @Autowired
    private IQuestionService iQuestionService;
    @Autowired
    private IQuestionTypeService iQuestionTypeService;
    @Autowired
    private ITraineeService traineeService;
    @Autowired
    private IPlanService planService;

    /**
     * 首页
     */
    @GetMapping("")
    public Result<Object> index(){
        Result<Object> result = Result.OK();
        try {
            Map<String , Object> resultMap = new HashMap<>();
            // 查询问题总数
            int questionCount = iQuestionService.count();
            resultMap.put("questionCount" , questionCount);
            // 查询昨日问题总数
            String yesterday = DateUtils.formatAddDate("yyyy-MM-dd", -24);
            int questionYesterdayCount = iQuestionService.count(new QueryWrapper<Question>()
                .lambda().ge(Question::getCreateTime, yesterday+" 00:00:00")
                .le(Question::getCreateTime, yesterday+" 23:59:59"));
            resultMap.put("questionYesterdayCount" , questionYesterdayCount);
            // 查询今日问题总数
            String toDay = DateUtils.getDateStr("yyyy-MM-dd");
            int questionTodayCount = iQuestionService.count(new QueryWrapper<Question>()
                    .lambda().ge(Question::getCreateTime, toDay+" 00:00:00")
                    .le(Question::getCreateTime, toDay+" 23:59:59"));
            resultMap.put("questionTodayCount" , questionTodayCount);
            //查询学员总数
            int userCount = traineeService.count();
            resultMap.put("userCount" , userCount);
            //查询学员年龄分布
            List<TraineeAgeDistDTO> traineeAgeDistDTOList = traineeService.distAge(new QuestionQueryPageDTO());
            resultMap.put("traineeAgeDistDTOList" , traineeAgeDistDTOList);
            //查询方案总数
            int planCount = planService.count();
            resultMap.put("planCount" , planCount);
            //查询7天方案分布
            String sevenday = DateUtils.formatAddDate("yyyy-MM-dd", -24 * 7);
            Map<String, List<Plan>> planMap = planService.list(new QueryWrapper<Plan>()
                    .lambda().ge(Plan::getCreateTime, sevenday+" 00:00:00"))
                    .stream().collect(Collectors.groupingBy(i->DateUtils.date2Str(i.getCreateTime(), "MM-dd")));
            Map<String, Integer> everyDayPlanMap = new HashMap<>();
            for (int i = 7; i >= 1; i--) {
                String everyDay = DateUtils.formatAddDate("MM-dd", -24 * i);
                if(planMap.containsKey(everyDay)){
                    everyDayPlanMap.put(everyDay, planMap.get(everyDay).size());
                }else {
                    everyDayPlanMap.put(everyDay, 0);
                }
            }
            resultMap.put("everyDayPlanMap" , everyDayPlanMap);
            //查询14问题
            String fourteenday = DateUtils.formatAddDate("yyyy-MM-dd", -24 * 14);
            Map<String, List<Question>> questionMap = iQuestionService.list(new QueryWrapper<Question>()
                    .lambda().ge(Question::getCreateTime, fourteenday+" 00:00:00"))
                    .stream().collect(Collectors.groupingBy(i->DateUtils.date2Str(i.getCreateTime(), "MM-dd")));
            Map<String, Integer> everyDayQuestionMap = new HashMap<>();
            for (int i = 14; i >= 1; i--) {
                String everyDay = DateUtils.formatAddDate("MM-dd", -24 * i);
                if(questionMap.containsKey(everyDay)){
                    everyDayQuestionMap.put(everyDay, questionMap.get(everyDay).size());
                }else {
                    everyDayQuestionMap.put(everyDay, 0);
                }
            }
            resultMap.put("everyDayQuestionMap" , everyDayQuestionMap);
            //查询高频问题
            List<Map<String, String>> questionTypes = iQuestionTypeService.queryTypeByUserId(null);
            resultMap.put("questionTypes" , questionTypes);
            result.setResult(resultMap);
        } catch (ParseException e) {
            result = Result.error("日期获取异常");
            e.printStackTrace();
        } catch (Exception e) {
            result = Result.error("系统异常");
        }
        return result;
    }
}
